package com.icss.clothes.cotroller;

import com.icss.clothes.entity.Order_detail;
import com.icss.clothes.entity.Orders;
import com.icss.clothes.entity.User;
import com.icss.clothes.service.imp.OrderService;
import com.icss.clothes.util.AliPayUtil;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller
public class OrderController {
    @Autowired
    private OrderService orderService;
    //用于跳转到我的订单页面的控制器
    @RequestMapping("/toOrder")
    public ModelAndView toOrder(HttpSession session){
        ModelAndView modelAndView=new ModelAndView("order");
        User user=(User) session.getAttribute("loginUser");
        int user_id=user.getUser_id();
        //需要有一个能够通过指定的用户编号查询得到所有订单信息的业务逻辑
        List<Orders> ordersList=orderService.findAllByUser_id(user_id);
        modelAndView.addObject("ordersList",ordersList);
        return modelAndView;
    }
    //用于跳转到订单详情页面的控制器
    @RequestMapping("/toOrderDetail")
    public ModelAndView toOrderDetail(String order_id){
        ModelAndView modelAndView=new ModelAndView("order_detail");
        //需要一个能够通过指定的订单编号查询得到订单详情的业务逻辑
        List<Order_detail> orderDetails=orderService.findOrderDetailAllByOrder_id(order_id);
        modelAndView.addObject("orderDetails",orderDetails);
        return modelAndView;
    }
    //用于取消订单的控制器
    @RequestMapping("/deleteOrder")
    @ResponseBody
    public ModelAndView deleteOrder(String order_id){
        ModelAndView modelAndView=new ModelAndView("order");
        //需要一个能够实现取消订单的业务逻辑
        orderService.deleteOrder(order_id);
        return modelAndView;
    }
    //跳转到支付宝页面的控制器
    @RequestMapping("/toPay")
    public void toPay(String order_id, int address_id, double order_price, HttpServletResponse response){
        //通过指定的商品编号向商品表中插入指定的收货地址id
        orderService.updateAddressByOrder_id(order_id,address_id);
        String pageRedirectionData= AliPayUtil.toPay(order_id,order_price);
        response.setCharacterEncoding("utf8");
        response.setContentType("text/html");
        PrintWriter printWriter= null;
        try {
            printWriter = response.getWriter();
            printWriter.print(pageRedirectionData);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    @RequestMapping("/returnPay")
    public ModelAndView returnPay(String out_trade_no){
        ModelAndView modelAndView=new ModelAndView("paySuccess");
        //需要一个能够通过指定的订单编号修改指定的订单状态的业务逻辑
        orderService.updateStateByOrder_id(out_trade_no,1);
        modelAndView.addObject("order_id",out_trade_no);
        return modelAndView;
    }
}
